package ui.face {

	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import globals.Global;
	
	import ui.component.Button;
	import ui.component.ImageButton;
	import ui.component.Label;
	import ui.panel.PanelType;
	
	import util.UIUtil;
	
	public class BottomFace extends Sprite {
		
		private var friendList:Array;
		private var actionList:Array;
		private var monsterLabel:Label;
		private var mapLabel:Label;
		private var waveLabel:Label;
		private var refreshButton:Button;

		public function BottomFace() {
			super();
			
			var bg:Bitmap;
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("bottom"));
			addChild(bg);
			
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("bottom_stick"));
			bg.x = 630;
			bg.y = -6;
			addChild(bg);
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("bottom_stick"));
			bg.x = 630;
			bg.y = 44;
			addChild(bg);
			
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("wave_icon"));
			bg.x = 650;
			bg.y = 4;
			addChild(bg);
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("map_icon"));
			bg.x = 650;
			bg.y = 58;
			addChild(bg);
			
			monsterLabel = new Label(16, 0xFFFFFF, true, "left", true);
			monsterLabel.x = 680;
			monsterLabel.y = 6;
			addChild(monsterLabel);
			mapLabel = new Label(12, 0xFFFFFF, true, "left", true);
			mapLabel.x = 680;
			mapLabel.y = 60;
			addChild(mapLabel);
			
			friendList = new Array();
			var i:uint;
			var friend:FriendCell;
			for (i = 0; i<8; i++) {
				friend = new FriendCell();
				friend.x = 58 + 65 * i;
				friend.y = 14;
				addChild(friend);
				friendList.push(friend);
			}
			
			actionList = new Array();
			var actionIcon:ActionIcon;
			
			actionIcon = new ActionIcon("sc_tower", actionHandler);
			actionIcon.addItem("sc_build", "建造", buildHandler);
			actionIcon.addItem("sc_distroy", "拆除", presenceHandler);
			actionIcon.x = 55;
			actionIcon.y = -56;
			addChild(actionIcon);
			actionList.push(actionIcon);
			
			actionIcon = new ActionIcon("sc_warrior", actionHandler);
			actionIcon.addItem("sc_train", "训练", trainHandler);
			actionIcon.addItem("sc_presence", "装备", equipHandler);
			actionIcon.x = 115;
			actionIcon.y = -56;
			addChild(actionIcon);
			actionList.push(actionIcon);
			
			actionIcon = new ActionIcon("sc_equip", actionHandler);
			actionIcon.addItem("sc_equipon", "合成", equipHandler);
			actionIcon.addItem("sc_strong", "强化", presenceHandler);
			actionIcon.x = 175;
			actionIcon.y = -56;
			addChild(actionIcon);
			actionList.push(actionIcon);
			
			actionIcon = new ActionIcon("sc_item", actionHandler);
			actionIcon.addItem("sc_trade", "购买", levelupHandler);
			actionIcon.addItem("sc_bag", "背包", presenceHandler);
			actionIcon.x = 235;
			actionIcon.y = -56;
			addChild(actionIcon);
			actionList.push(actionIcon);
			
			refreshButton = UIUtil.getButton(["friend_refresh_off", "friend_refresh_on", "friend_refresh_off"], refreshHandler);
			refreshButton.x = 580;
			refreshButton.y = 66;
			addChild(refreshButton);
			
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("wave_button_off"));
			bg.x = 668;
			bg.y = -70;
			addChild(bg);
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("wave_sword"));
			bg.x = 664;
			bg.y = -74;
			addChild(bg);
			
			var effect:MovieClip = Global.assetsManager.getAssetsAsMovieClip("BladeGlow", "effect");
			effect.x = 706;
			effect.y = -32;
			addChild(effect);
			
			waveLabel = new Label(12, 0xFFFFFF, true, "left", true);
			waveLabel.x = 674;
			waveLabel.y = -38;
			addChild(waveLabel);
			
			updataFriend(null);
			updateWave("11/11");
		}
		
		private function actionHandler(item:ActionIcon):void {
			for each (var actionIcon:ActionIcon in actionList) {
				if (actionIcon != item || actionIcon.opened) {
					actionIcon.layout(false);
				} else {
					actionIcon.layout(true);
				}
			}
		}
		
		private function closeAllAction():void {
			for each (var actionIcon:ActionIcon in actionList) {
				actionIcon.layout(false);
			}
		}
		
		public function updateMap(data:String):void {
			mapLabel.text = data;
		}
		
		public function updateWave(data:String):void {
			waveLabel.text = data;
		}
		
		public function updateMonster(data:uint):void {
			monsterLabel.text = data.toString();
		}
		
		private function refreshHandler(button:Button):void {
			
		}
		
		private function levelupHandler():void {
		}
		
		private function buildHandler():void {
			Global.uiManager.openPanel(PanelType.TOWER_PANEL);
		}
		
		private function presenceHandler():void {
			Global.uiManager.openPanel(PanelType.PRESENCE_PANEL);
		}
		
		private function equipHandler():void {
			Global.uiManager.openPanel(PanelType.EQUIP_PANEL);
		}
		
		private function trainHandler():void {
			Global.uiManager.openPanel(PanelType.WARRIOR_PANEL);
		}
		
		public function updataFriend(data:Array):void {
			var friend:FriendCell;
			for (var i:int = 0; i<8; i++) {
				friend = friendList[i];
				if (i < 4) {
					friend.update("remote/avatar1.png", "neaamo");
				} else {
					friend.update("", "");
				}
			}
		}
	}
}